package com.cjb.cloudsecurity.userMsg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjb.cloudsecurity.userMsg.entity.SysOrg;
import com.cjb.cloudsecurity.userMsg.mapper.SysOrgMapper;
import com.cjb.cloudsecurity.userMsg.service.SysOrgService;
import com.cjb.cloudsecurity.userMsg.vo.SysOrgVO;
import com.cjb.common.mybatisUtil.impl.BaseRepositoryImpl;
import com.cjb.common.query.DelEnum;
import com.cjb.common.util.Common;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @Author chenjinbin
* @Package IntelliJ IDEA
* @Class SysOrgsServiceImpl
* @Date 2023-07-27 21:41
*/
@Service
@AllArgsConstructor
public class SysOrgRepositoryImpl extends BaseRepositoryImpl<SysOrgMapper, SysOrg> implements SysOrgService {
/**
* 日志
*/
    private final static Logger log = LoggerFactory.getLogger(SysOrgRepositoryImpl.class);
    /*
    * 注入mapper层
    */
    private final SysOrgMapper sysOrgMapper;
    /**
    * 单体保存
    *
    * @Author chenjinbin
    * @Value
    * @Title
    */
    @Override
    public boolean save(SysOrg sysOrg) {
        baseMapper.insert(sysOrg);
        return true;
    }

    /**
    *批量保存
    *
    * @Author chenjinbin
    * @Value
    * @Title
    */
    @Override
    public Integer listSave(List<SysOrg> sysOrg) {
        return sysOrgMapper.insertBatchSomeColumn(sysOrg);
    }

    /**
    * 批量更新
    *
    * @Author chenjinbin
    * @Value
    * @Title
    */
    @Override
    public void listUpdate(List<SysOrg> sysOrg) {
        sysOrgMapper.insertBatchSomeColumn(sysOrg);
    }

    /**
    * 批量删除
    *
    * @Author chenjinbin
    * @Value
    * @Title
    */
    @Override
    public void listDelete(List<SysOrg> sysOrg) {
        List<Long> collect = sysOrg.stream().map(SysOrg::getId)
        .distinct()
        .collect(Collectors.toList());
        sysOrgMapper.deleteBatchIds(collect);
    }
    /**
    * 通过id删除
    *
    * @Author chenjinbin
    * @Value
    * @Title
    */
    @Override
    public int deleteById(SysOrg sysOrg) {
    return sysOrgMapper.deleteById(sysOrg.getId());
    }

    /**
    *单个查询
    *
    * @Author chenjinbin
    * @Value
    * @Title
    */
    @Override
    public SysOrg queryById(SysOrg sysOrg) {
        return  sysOrgMapper.selectById(sysOrg.getId());
    }

    /**
     *分页
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public Page queryPage(Page sysOrgPage , SysOrgVO sysOrg) {
        return sysOrgMapper.selectPage(sysOrgPage, getWrapper(sysOrg));
    }
    /**
     *查询属性
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    private LambdaQueryWrapper<SysOrg> getWrapper(SysOrgVO query) {
        LambdaQueryWrapper<SysOrg> wrapper = Wrappers.lambdaQuery();
        if (Common.isNotNull(query.getId())) {
            wrapper.eq(SysOrg::getId, query.getId());
        }
        if (Common.isNotNull(query.getPid())) {
            wrapper.eq(SysOrg::getPid, query.getPid());
        }
        if (Common.isNotNull(query.getName())) {
            wrapper.eq(SysOrg::getName, query.getName());
        }
        if (Common.isNotNull(query.getSort())) {
            wrapper.eq(SysOrg::getSort, query.getSort());
        }
        if (Common.isNotNull(query.getTenantId())) {
            wrapper.eq(SysOrg::getTenantId, query.getTenantId());
        }
        if (Common.isNotNull(query.getVersion())) {
            wrapper.eq(SysOrg::getVersion, query.getVersion());
        }
        wrapper.eq(SysOrg::getDeleted, DelEnum.NO_DEL.getCode());
        if (Common.isNotNull(query.getCreator())) {
            wrapper.eq(SysOrg::getCreator, query.getCreator());
        }
        if (Common.isNotNull(query.getCreateTime())) {
            wrapper.eq(SysOrg::getCreateTime, query.getCreateTime());
        }
        if (Common.isNotNull(query.getUpdater())) {
            wrapper.eq(SysOrg::getUpdater, query.getUpdater());
        }
        if (Common.isNotNull(query.getUpdateTime())) {
            wrapper.eq(SysOrg::getUpdateTime, query.getUpdateTime());
        }
        wrapper.orderByDesc(SysOrg::getId);
        return wrapper;
    }
    /**
    * 返回全部信息
    *
    * @Author chenjinbin
    * @Value
    * @Title
    */
    @Override
    public List<SysOrg> returnAllData() {
        return sysOrgMapper.selectList(null);
    }

    /**
     * 获得子机构
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public List<Long> getSubOrgIdList(Long id) {
        // 所有机构的id、pid列表
        List<SysOrg> orgList = baseMapper.getIdAndPidList();

        // 递归查询所有子机构ID列表
        List<Long> subIdList = new ArrayList<>();
        getTree(id, orgList, subIdList);

        // 本机构也添加进去
        subIdList.add(id);

        return subIdList;
    }
    /**
     * 组织树
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    private void getTree(Long id, List<SysOrg> orgList, List<Long> subIdList) {
        for(SysOrg org : orgList){
            if (org.getPid().equals(id)){
                getTree(org.getId(), orgList, subIdList);
                subIdList.add(org.getId());
            }
        }
    }


}